#read in the csv gender inequality and the geojson world map

library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(here)
## here() starts at /Users/luissorto/Desktop/Geographic Information Systems & Science/Week 4/GISSprac4
library(dplyr)
library(janitor)
## 
## Attaching package: 'janitor'
## 
## The following objects are masked from 'package:stats':
## 
##     chisq.test, fisher.test
library(readr)
library(tidyr)
library(countrycode)
library(tmap)
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
composite_indices <- read.csv('composite_indices.csv')

world_map <- st_read('World_Countries.geojson')
## Reading layer `World_Countries' from data source 
##   `/Users/luissorto/Desktop/Geographic Information Systems & Science/Week 4/GISSprac4/World_Countries.geojson' 
##   using driver `GeoJSON'
## Simple feature collection with 251 features and 5 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -180 ymin: -89 xmax: 180 ymax: 83.6236
## Geodetic CRS:  WGS 84

#filter the columns so only Gender Inequality Index for 2010 and 2019 are left for each country

gender_inequality <- composite_indices %>% 
  group_by(country) %>% 
  select(iso3, country, gii_2010, gii_2019)

gender_inequality
## # A tibble: 206 × 4
## # Groups:   country [206]
##    iso3  country             gii_2010 gii_2019
##    <chr> <chr>                  <dbl>    <dbl>
##  1 AFG   Afghanistan            0.707    0.676
##  2 ALB   Albania                0.192    0.131
##  3 DZA   Algeria                0.517    0.397
##  4 AND   Andorra               NA       NA    
##  5 AGO   Angola                 0.554    0.533
##  6 ATG   Antigua and Barbuda   NA       NA    
##  7 ARG   Argentina              0.372    0.291
##  8 ARM   Armenia                0.346    0.219
##  9 AUS   Australia              0.136    0.08 
## 10 AUT   Austria                0.11     0.053
## # ℹ 196 more rows

#Now, create an extra column that looks at the gii difference between 2019 and 2010

gender_inequality_diff <- gender_inequality %>% 
  mutate(gii_diff = (gii_2019 - gii_2010))

gender_inequality_diff
## # A tibble: 206 × 5
## # Groups:   country [206]
##    iso3  country             gii_2010 gii_2019 gii_diff
##    <chr> <chr>                  <dbl>    <dbl>    <dbl>
##  1 AFG   Afghanistan            0.707    0.676  -0.0310
##  2 ALB   Albania                0.192    0.131  -0.061 
##  3 DZA   Algeria                0.517    0.397  -0.12  
##  4 AND   Andorra               NA       NA      NA     
##  5 AGO   Angola                 0.554    0.533  -0.0210
##  6 ATG   Antigua and Barbuda   NA       NA      NA     
##  7 ARG   Argentina              0.372    0.291  -0.081 
##  8 ARM   Armenia                0.346    0.219  -0.127 
##  9 AUS   Australia              0.136    0.08   -0.056 
## 10 AUT   Austria                0.11     0.053  -0.057 
## # ℹ 196 more rows

#Now, we have to join the World Map data with the new Gender Inequality Diff table - however, we have to make the column iso3 codes the same. For that, we’ll use the countrycode package

#transform the country code in the gender inequality file to match that of the world map file
#then remove all the regions/non-country rows

gender_inequality_diff$iso2 <- countrycode(gender_inequality_diff$iso3, origin = "iso3c", destination = "iso2c") 
## Warning: Some values were not matched unambiguously: ZZA.VHHD, ZZB.HHD, ZZC.MHD, ZZD.LHD, ZZE.AS, ZZF.EAP, ZZG.ECA, ZZH.LAC, ZZI.SA, ZZJ.SSA, ZZK.WORLD
gender_inequality_diff <- gender_inequality_diff %>% 
  filter(iso2 != "NA")

gender_inequality_diff
## # A tibble: 194 × 6
## # Groups:   country [194]
##    iso3  country             gii_2010 gii_2019 gii_diff iso2 
##    <chr> <chr>                  <dbl>    <dbl>    <dbl> <chr>
##  1 AFG   Afghanistan            0.707    0.676  -0.0310 AF   
##  2 ALB   Albania                0.192    0.131  -0.061  AL   
##  3 DZA   Algeria                0.517    0.397  -0.12   DZ   
##  4 AND   Andorra               NA       NA      NA      AD   
##  5 AGO   Angola                 0.554    0.533  -0.0210 AO   
##  6 ATG   Antigua and Barbuda   NA       NA      NA      AG   
##  7 ARG   Argentina              0.372    0.291  -0.081  AR   
##  8 ARM   Armenia                0.346    0.219  -0.127  AM   
##  9 AUS   Australia              0.136    0.08   -0.056  AU   
## 10 AUT   Austria                0.11     0.053  -0.057  AT   
## # ℹ 184 more rows

#Join the world map file to the gender inequality updated file

gender_inequality_diff_map <- world_map %>% 
  inner_join(.,
             gender_inequality_diff,
             by = c("ISO" = "iso2"))

gender_inequality_diff_map
## Simple feature collection with 196 features and 10 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -180 ymin: -55.90223 xmax: 180 ymax: 83.11387
## Geodetic CRS:  WGS 84
## First 10 features:
##    FID             COUNTRY ISO          COUNTRYAFF AFF_ISO iso3
## 1    1         Afghanistan  AF         Afghanistan      AF  AFG
## 2    2             Albania  AL             Albania      AL  ALB
## 3    3             Algeria  DZ             Algeria      DZ  DZA
## 4    5             Andorra  AD             Andorra      AD  AND
## 5    6              Angola  AO              Angola      AO  AGO
## 6    9 Antigua and Barbuda  AG Antigua and Barbuda      AG  ATG
## 7   10           Argentina  AR           Argentina      AR  ARG
## 8   11             Armenia  AM             Armenia      AM  ARM
## 9   13           Australia  AU           Australia      AU  AUS
## 10  14             Austria  AT             Austria      AT  AUT
##                country gii_2010 gii_2019 gii_diff
## 1          Afghanistan    0.707    0.676   -0.031
## 2              Albania    0.192    0.131   -0.061
## 3              Algeria    0.517    0.397   -0.120
## 4              Andorra       NA       NA       NA
## 5               Angola    0.554    0.533   -0.021
## 6  Antigua and Barbuda       NA       NA       NA
## 7            Argentina    0.372    0.291   -0.081
## 8              Armenia    0.346    0.219   -0.127
## 9            Australia    0.136    0.080   -0.056
## 10             Austria    0.110    0.053   -0.057
##                          geometry
## 1  MULTIPOLYGON (((61.27655 35...
## 2  MULTIPOLYGON (((19.57083 41...
## 3  MULTIPOLYGON (((4.603354 36...
## 4  MULTIPOLYGON (((1.445836 42...
## 5  MULTIPOLYGON (((23.47611 -1...
## 6  MULTIPOLYGON (((-61.73806 1...
## 7  MULTIPOLYGON (((-71.85916 -...
## 8  MULTIPOLYGON (((46.54037 38...
## 9  MULTIPOLYGON (((151.5403 -2...
## 10 MULTIPOLYGON (((10.47124 46...

#Plot the map! We’ll use the “view” mode to make it interactive

tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(gender_inequality_diff_map) +
  tm_polygons("gii_diff",
              palette = "-Spectral",
              title = "GII Difference, 2010-2019",
              id = "COUNTRY",
              alpha = 0.5,
              midpoint = NA) 

#If we ant to use plot mode, we can use the following code - added in code to make the legend smaller

tmap_mode("plot")
## tmap mode set to plotting
tm_shape(gender_inequality_diff_map) +
  tm_polygons("gii_diff",
              palette = "-Spectral",
              title = "GII Difference, 2010-2019",
              id = "COUNTRY",
              alpha = 0.5,
              midpoint = NA) +
  tm_legend(
    legend.text.size = 0.6, 
    legend.title.size = 0.8,
    legend.position = c("left", "bottom")
  )